package com.ly.airline.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @author ：xxl
 * @ClassName HttpResult
 * @date ：Created in 2022/3/15 1:17
 * @description： 统一返回类
 * @modified By：
 * @version: 1.0
 */
@Data
public class HttpResult<T> implements Serializable {

    /**
     * 是否响应成功
     */
    private Boolean success;
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 错误信息
     */
    private String message;

    // 构造器开始
    /**
     * 无参构造器(构造器私有，外部不可以直接创建)
     */
    private HttpResult() {
        this.code = 200;
        this.success = true;
    }
    /**
     * 有参构造器
     * @param obj
     */
    private HttpResult(T obj) {
        this.code = 200;
        this.data = obj;
        this.success = true;
    }

    private HttpResult(T obj, String message) {
        this.code = 200;
        this.data = obj;
        this.success = true;
        this.message=message;
    }

    private HttpResult(boolean success, String message) {
        this.code = 200;
        this.success = success;
        this.message=message;
    }

    private HttpResult(Integer code) {
        this.code = code;
//        this.success = success;
//        this.message=message;
    }

    /**
     * 有参构造器
     * @param resultCode
     */
    private HttpResult(ResultCodeEnum resultCode) {
        this.success = false;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }
    // 构造器结束

    /**
     * 通用返回成功（没有返回结果）
     * @param <T>
     * @return
     */
    public static<T> HttpResult<T> success(){
        return new HttpResult();
    }

    /**
     * 返回成功（有返回结果）
     * @param data
     * @param <T>
     * @return
     */
    public static<T> HttpResult<T> success(T data){
        return new HttpResult<T>(data);
    }

//    public static<T> HttpResult<T> success(T data, String message){
//        return new HttpResult<T>(data,message);
//    }

    public static<T> HttpResult<T> success(boolean success, String message){
        return new HttpResult<T>(success,message);
    }

    public static<T> HttpResult<T> success(Integer code){
        return new HttpResult<T>(code);
    }

    /**
     * 通用返回失败
     * @param resultCode
     * @param <T>
     * @return
     */
    public static<T> HttpResult<T> failure(ResultCodeEnum resultCode){
        return  new HttpResult<T>(resultCode);
    }




}
